package com.rural.bus.driver.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.rural.bus.driver.BaseActivity;
import com.rural.bus.driver.MyApplication;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;
import com.rural.bus.driver.utils.SuUtil;

public class User {
	public String drivId;
	public String name;
	public String phone;
	
	/***
	 * loginin
	 * @param context
	 * @param phonenum
	 * @param psw
	 * @param callback
	 */
	public void login(final Context context,String phonenum,String psw,final OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("phone", phonenum);
		http.addBodyParm("password", psw);
		http.send(Url.login_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context,object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});
	}
	
	/***
	 * parse json receive from internet
	 * @param context
	 * @param object
	 */
	private void parse(Context context,JSONObject object){
		if(object==null){
			return;
		}
		try {
			JSONObject o=object.getJSONObject("baseInfo");
			this.name=o.getString("name");
			this.phone=o.getString("phone");
			this.drivId=o.getString("drivId");
			
			User.saveUserToPref(context, this);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/***
	 * save user to sharedpreference
	 * @param context
	 * @param user
	 */
	public static void saveUserToPref(Context context,User user){
		SuUtil su=new SuUtil(context, "_user");
		su.putValueByKey("userId", user.drivId);
		su.putValueByKey("name", user.name);
		su.putValueByKey("phone", user.phone);
	}
	
	/***
	 * logout
	 * @param context
	 */
	public static void logout(Context context){
//		MyApplication app=(MyApplication) context.getApplicationContext();
//		app.setCurrentUser(null);
//		BaseActivity activity=(BaseActivity) context;
//		activity.mCurrentUser=null;
//		
//		SuUtil su=new SuUtil(context, "_user");
//		su.clear();
	}
	
	/***
	 * get user from sharedpreference
	 * @param context
	 * @return
	 */
	public static User getUserFromPref(Context context){
		User user=new User();
		SuUtil su=new SuUtil(context, "_user");
		user.drivId=su.getValueByKey("userId", null);
		user.name=su.getValueByKey("name", null);
		user.phone=su.getValueByKey("phone", null);
		if(TextUtils.isEmpty(user.drivId)){
			return null;
		}
        return user;
	}
	/***
	 * 用户注册
	 */
	public void register(final Context context,String name,String password,String phone,
			String regionId,String remarks,int sex,final OnNetCallback callback){
		
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("name", name);
		http.addBodyParm("password", password);
		http.addBodyParm("phone", phone);
		http.addBodyParm("regionId", regionId);
		http.addBodyParm("remarks", remarks);
		
		http.addBodyParm("sex", ""+sex);
		http.send(Url.register_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context,object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});

	}
	
	public void update(double currentLongtitude,double currentLatitude,int status,String regionId,OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("driverId", ""+drivId);
		http.addBodyParm("currentLongtitude", ""+currentLongtitude);
		http.addBodyParm("currentLatitude", ""+currentLatitude);
		http.addBodyParm("status", ""+status);
		http.addBodyParm("regionId", regionId);

		http.send(Url.update_url, callback);
	}
	
	public void getOrderList(int start,int end,int status,OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("udid", ""+drivId);
		http.addBodyParm("type", ""+1);
		http.addBodyParm("start", ""+start);
		http.addBodyParm("end", ""+end);
//		http.addBodyParm("status", ""+status);
		
		http.send(Url.orderlist_url, callback);

	}

}
